package com.hjw.hjwmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjw.common.utils.PageUtils;
import com.hjw.common.utils.Query;

import com.hjw.hjwmall.product.dao.CategoryDao;
import com.hjw.hjwmall.product.entity.CategoryEntity;
import com.hjw.hjwmall.product.service.CategoryService;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        // 1.查询出所有的分类数据
        List<CategoryEntity> entityList = list();
        // 为空返回空数组
        if (CollectionUtils.isEmpty(entityList)) {
            return Collections.emptyList();
        }
        // 2.组装数据为树形结构返回

        // 2.1 获取所有的一级分类，构建子集，排序返回
        return entityList.stream()
                .filter(categoryEntity -> categoryEntity.getParentCid() == 0)
                .peek(category -> category.setChildren(getChildren(category, entityList)))
                .sorted(Comparator.comparingInt(category -> (Objects.isNull(category.getSort()) ? 0 : category.getSort())))
                .collect(Collectors.toList());
    }

    @Override
    public boolean removeCategoryByIds(@NotNull List<Long> catIds) {
        // 删除关联信息

        // 删除分类
        return removeByIds(catIds);
    }

    @Override
    public boolean saveCategory(@NotNull CategoryEntity category) {
        CategoryEntity categoryEntity = getCategoryByName(category.getName());
        return !Objects.nonNull(categoryEntity) && save(category);
    }

    @Override
    public CategoryEntity getCategoryByName(@NotBlank String categoryName) {
        LambdaQueryWrapper<CategoryEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CategoryEntity::getName, categoryName);
        return getOne(queryWrapper);
    }

    /**
     * 递归查询所有菜单的子菜单
     *
     * @param root       菜单对象
     * @param entityList 目录集合对象
     * @return List<CategoryEntity> 菜单集合对象的子菜单
     */
    private List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> entityList) {
        return entityList.stream()
                .filter(categoryEntity -> Objects.equals(categoryEntity.getParentCid(), root.getCatId())
                ).peek(categoryEntity -> {
                    // 1.找到子类集合
                    categoryEntity.setChildren(getChildren(categoryEntity, entityList));
                }).sorted(
                        Comparator.comparingInt(category -> (Objects.isNull(category.getSort()) ? 0 : category.getSort()))
                ).collect(Collectors.toList());
    }

}